﻿using Core.DomainServices;
using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Infrastructure.DomainServices
{
    public class DocumentService : IDocumentService
    {
        private readonly IAppConfig appConfig;
        private readonly IDataRepository repository;

        public DocumentService(IDataRepository repository, IAppConfig appConfig)
        {
            this.repository = repository;
            this.appConfig = appConfig;
        }

        public string CreateFileFromTemplate(string empId, string outputFolder)
        {
            string destFilename = outputFolder + Path.DirectorySeparatorChar + empId + "-"+ DateTime.Now.ToString("ddMMyyyy hhmmss") + Path.GetExtension(appConfig.TemplatePath);
            File.Copy(appConfig.TemplatePath, destFilename);
            return destFilename;
        }

        public IList<string> CreateTimesheets(string outputFolder)
        {
            var items = repository.GetAll();
            var timesheets = repository.GetTimesheets(items);

            var results = new List<string>();
            if(timesheets != null && timesheets.Any())
            {
                var employeeIds = timesheets.Select(d => d.EmployeeId).Distinct();
                foreach (var empId in employeeIds)
                {
                    string fileName = CreateFileFromTemplate(empId, outputFolder);
                    results.Add(fileName);
                    var tses = timesheets.Where(d => d.EmployeeId == empId).ToList();
                    
                    using (var pck = new ExcelPackage(new FileInfo(fileName)))
                    {
                        ExcelWorksheet ws = pck.Workbook.Worksheets["C001"];
                        if (tses != null && tses.Any())
                        {
                            int rowIndex = 11;
                            foreach (var item in tses)
                            {
                                ws.Cells["A" + rowIndex].Value = item.Day.Value.ToString("MM/dd/yyyy");
                                ws.Cells["B" + rowIndex].Value = item.Comment;
                                ws.Cells["H" + rowIndex].Value = item.STHours;
                                ws.Cells["I" + rowIndex].Value = item.OTHours;

                                rowIndex++;
                            }
                        }

                        ExcelWorksheet tally = pck.Workbook.Worksheets["Tally"];
                        if (tses != null && tses.Any())
                        {
                            int rowIndex = 3;
                            tally.Cells["A" + rowIndex].Value = tses.First().EmployeeId;
                            tally.Cells["C" + rowIndex].Value = tses.First().EmployeeId;
                            tally.Cells["F" + rowIndex].Value = tses.Sum(d=>d.STHours);
                            tally.Cells["G" + rowIndex].Value = tses.Sum(d => d.OTHours);
                        }
                        pck.Save();                        
                    }
                }
            }

            return results;
        }
    }
}
